package exam;

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串：");
        String str = scanner.nextLine();
        System.out.println("请输入匹配串：");
        String str1 = scanner.nextLine();
        scanner.close();

        int res = isMatch(str,str1);
        System.out.println(res);
    }

    private static int isMatch(String str, String str1) {
        return isMatchHelper(str,0,str1,0);
    }

    private static int isMatchHelper(String str, int sIndex, String str1, int pIndex) {
       if(sIndex == str.length() && pIndex == str1.length()){
           return 1;
       }

       if(pIndex >= str1.length()){
           return 0;
       }

       if(pIndex >= str.length()){
           if(pIndex +1 < str1.length() && str1.charAt(pIndex + 1) == '*'){
               return isMatchHelper(str,sIndex,str1,pIndex + 2);
           }
           return 0;
       }
       char sChar = str.charAt(sIndex);
       char pChar = str1.charAt(pIndex);

       if(pIndex + 1 < str1.length() && str1.charAt(pIndex + 1) == '*'){
           if(pChar == '.' || pChar == sChar){
               return  isMatchHelper(str,sIndex + 1,str1,pIndex + 2);
           }else{
               return  isMatchHelper(str,sIndex,str1,pIndex + 2);
           }
       }else{
           if(pChar == '.' || pChar == sChar){
               return  isMatchHelper(str,sIndex + 1,str1,pIndex + 1);
           }else{
               return  0;
           }
       }
    }
}
